package com.hdl.servlet.article;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hdl.R;
import com.hdl.entity.Article;
import com.hdl.service.ArticleService;
import com.hdl.service.impl.ArticleServiceImpl;

@WebServlet("/admin-article-update.let")
public class ArticleUpdateServlet extends HttpServlet{

	Logger log = LoggerFactory.getLogger(this.getClass());
	private ArticleService articleService = new ArticleServiceImpl();
	private Article article = new Article();
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		article.setArticleTitle(req.getParameter("articleTitle"));
		article.setArticleContent(req.getParameter("articleContent"));
		System.out.println(article);
		log.debug(JSON.toJSONString(article));
		articleService.update(article);
		Object data = R.ok("修改文章成功");
		req.setAttribute("data",data );
		ObjectMapper mapper = new ObjectMapper();
		resp.setContentType("application/json;charset=UTF-8");
		mapper.writeValue(resp.getOutputStream(),data );
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
}
